package controller;

import mapper.StudentMapper;
import model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import service.StudentService;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Student")
public class StudentController {
    //自动注入service层的对象studentService
    @Autowired
    StudentService studentService;
    @RequestMapping("list.do")
    public String list( Model model,@RequestParam(defaultValue = "1") int currPage,@RequestParam(defaultValue = "3") int pageSize){
/*        //Student s1=new Student("a","a",12);
        //Student s2=new Student("b","b",21);
        //取session有效的students
        List<Student> students= (List<Student>) session.getAttribute("students");
       //如果是第一次存数据，那么students为空，则需要新建对象
        if(students==null)
           students=new ArrayList<Student>();
        //将用户数据的学生信息添加到students列表里
        students.add(student);
        //把students存成session有效的对象
        session.setAttribute("students",students);
       // model.addAttribute("students",students);*/
        //调用studentMapper的findAll方法，返回数据库中student表的所有数据
      //  List<Student> students=studentService.findAll();
        List<Student> students=studentService.selectByPage(currPage-1<0?0:(currPage-1)*pageSize,pageSize);
        model.addAttribute("currPage",currPage<1?1:currPage);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("students",students);
        return "listStudents";
    }
    @RequestMapping(value = "add.do",method = RequestMethod.POST)
    public String add_post(Student student, Model model){
/*        System.out.println("post:your name is :"+student.getName()+" ,your password is :" +student.getPassword());
        //model是存放数据的对象
        model.addAttribute("student",student);
       // model.addAttribute("password",pswd);
        //addStudent.html是view
        return "addStudent";*/
        //调用studentMapper的insert方法
        studentService.add(student);
        //返回到list方法，显示student所有数据
        return list(model,1,3);
    }
    @RequestMapping(value = "add.do",method = RequestMethod.GET)
    public ModelAndView add_get(String uname,String pswd){
        System.out.println("get:your name is :"+uname+" ,your password is :" +pswd);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("uname",uname);
        modelAndView.setViewName("addStudent");
        return  modelAndView;
       // return "addStudent";
    }
    //删除
@RequestMapping("delete.do")
    public String delete(int id,Model model){
    studentService.delete(id);
        return list(model,1,3);
}
@RequestMapping("toUpdate.do")
    public String toUpdate(int id,Model model){
        //通过id查找到需要修改的学生信息
       Student student=studentService.getById(id);
       //准备将该学生信息传出到updateStudent.html
       model.addAttribute("student",student);
       return "updateStudent";
}
@RequestMapping("update.do")
    public String update(Student student,Model model){
    studentService.edit(student);
        return list(model,1,3);
}
@RequestMapping("login.do")
    public String login(HttpSession session,String name,String password,Model model){
        if(studentService.login(name,password))
        {
            session.setAttribute("username",name);
            return list(model,1,3);}
        else
            return "login";
}
}
